﻿#include "ComServerStatusWidget.h"


#pragma execution_character_set("utf-8")

ComServerStatusWidget::ComServerStatusWidget(const QString& title, QWidget *parent)
    : QGroupBox{title, parent}
{
    this->setMinimumWidth(200);
    mLayout = new QVBoxLayout();
    this->setLayout(mLayout);
}

void ComServerStatusWidget::removeOne(QString name)
{
    ComServerOneStatusWidget* wgt = mMapWgtComServerStatus.take(name);
    wgt->deleteLater();
    wgt = nullptr;
}

void ComServerStatusWidget::setStatus(QString name, QString status)
{
    mMapWgtComServerStatus.value(name)->setText(status);
}

void ComServerStatusWidget::updateConnectList(QString name, const QStringList &listClient)
{
    mMapWgtComServerStatus.value(name)->updateConnectList(listClient);
}

void ComServerStatusWidget::addOne(QString name)
{
    ComServerOneStatusWidget* wgt = new ComServerOneStatusWidget(name);
    mMapWgtComServerStatus.insert(name, wgt);
    mLayout->addWidget(wgt);
}


ComServerOneStatusWidget::ComServerOneStatusWidget(const QString &name, QWidget *parent)
{
    QGridLayout* gLayout = new QGridLayout();
    this->setLayout(gLayout);
    lblStatus = new QLabel("未连接");
    cbboxConnectList = new QComboBox();
    gLayout->addWidget(new QLabel(name), 0, 0);
    gLayout->addWidget(lblStatus, 0, 1);

    gLayout->addWidget(new QLabel("客户端"), 1, 0);
    gLayout->addWidget(cbboxConnectList, 1, 1);
}

void ComServerOneStatusWidget::setText(const QString &status)
{
    lblStatus->setText(status);
}
void ComServerOneStatusWidget::updateConnectList(const QStringList &listClient)
{
    for(auto ip : listClient){
        if(cbboxConnectList->findText(ip) == -1){
            cbboxConnectList->addItem(ip);
        }
    }
    int count = cbboxConnectList->count();
    for(int i = count - 1; i >= 0; --i){
        if(!listClient.contains(cbboxConnectList->itemText(i))){
            cbboxConnectList->removeItem(i);
        }
    }

}

